/**
 * 机构编辑窗口
 */
define(function(){
	var displayPassword = "~!@^#%$$#$@"; //修改用户时显示用的密码(要确保用户不会设置这样的密码。。。)
	var UserWindow = Ext.extend(Ext.Window, {
		resizable: false,
		modal: true,
		width: 400,
	    animateTarget: cu.getDefaultAnimateTarget(),
        bodyStyle:'padding:3px;',
		iconCls: 'icon-window',
		initComponent: function(){
			this.formPanel = this.createFormPanel();
			UserWindow.superclass.initComponent.call(Ext.apply(this, {
				items: this.formPanel,
				buttons: this.createButtons(),
				listeners:{
					show: function(){
						this.initFormData();
						this.getForm().firstFocus();
						cu.bindEnter(this, this.save);
					}
				}
			}));
		},
		modifiedId: null, //从构造器中传入，表示要编辑的对象的id，为空表示新增
		save: function(continueToAdd){
			//提交请求
			cu.submit({
				form: this.getForm(), 
				url: 'sys/user/save.do',
				before: function(form){
					//如果没有修改密码则清除，避免将显示用的密码保存
					if (this.getField('password').getValue() == displayPassword){
						this.getField('password').disable();
					}
				},
				success: function(data){
					if (this.callback){
						data.departName = this.getField('departId').getValueNode().attributes.text;//补充部门名称
						this.callback.call(this.cbScope, data);
					}
					if(continueToAdd){
						this.getField('name').setValue('');
						this.getField('loginName').setValue('');
						this.getField('password').setValue('');
						this.getField('password2').setValue('');
						this.getField('position').setValue('');
						this.getForm().clearInvalid();
					}
					else{
						this.close();
					}
				},
				scope: this
			});
		},
		initFormData: function(){
			if (!this.modifiedId) return;
			cu.get({
				url:'sys/user/get.do', 
				params: {id: this.modifiedId},
				success: function(data){
					this.getForm().setValues(data);
					//注意：修改用户时，密码明文是不可获取的，这里只是设置显示
					this.getField('password').setValue(displayPassword);
					this.getField('password2').setValue(displayPassword);
				},
				scope: this
			});
		},
		getForm: function(){
			return this.formPanel.getForm();
		},
		getField: function(name){
			return this.getForm().findField(name);
		},
		createFormPanel: function(){
			var formPanel = new Ext.form.FormPanel({
			    baseCls: 'x-plain',
			    bodyStyle:'padding:5px;',
			    labelWidth: 70,
			    labelPad: 5,
			    labelAlign: "right",
			    defaults: {anchor:'99%', style:'margin-bottom:5px;'},
			    items: [
			        {xtype:'hidden', name:'id'},
			        {
						xtype:'combotree',
						fieldLabel:'* 所属机构',
						hiddenName: 'departId',
						height: 200,
						dataUrl: 'sys/depart/getDepartTreeData.do?',
						nodePathUrl: 'sys/util/getEntityAttrPath.do?c=sys.Department',
						root: {id:'root', text:'根机构', expanded: true},
						rootVisible: true,
						rootSelectable: false,
						rootValue: null,
						showFullPath: true,
						allowBlank: false
			        },{
			        	xtype:'textfield',
			        	name: 'name',
			        	fieldLabel: '* 姓名',
						allowBlank: false,
			        	maxLength: 20, maxLengthText: '登陆名不能超过20个字'
			        },{
			        	xtype:'textfield',
			        	name: 'loginName',
			        	fieldLabel: '* 账号',
			        	maxLength: 20, maxLengthText: '登陆名不能超过20个字',
			        	allowBlank:false
			        },{ //注意：修改用户时不允许修改密码
			        	xtype:'textfield',
			        	inputType: 'password',
			        	name: 'password',
			        	fieldLabel: '* 密码',
			        	maxLength: 20, maxLengthText: '密码名不能超过20个字',
			        	allowBlank:false
			        },{
			        	xtype:'textfield',
			        	inputType: 'password',
			        	name: 'password2',
			        	fieldLabel: '* 密码确认',
			        	allowBlank:false,
			        	validator: function(value){
			        		var cmp = this.ownerCt.getForm().findField('password').getValue();
			        		return value == cmp ? true : "两次输入密码不一致";
			        	}
			        },{
			        	xtype:'combo',
			        	hiddenName: 'gender',
			        	fieldLabel: '* 性别',
			        	store: au.getDictItems('gender'),
						triggerAction: 'all',
						editable: false,
			        	allowBlank:false,
						value: 1
			        },{
			        	xtype:'textfield',
			        	name: 'position',
			        	fieldLabel: '岗位',
			        	maxLength: 30, maxLengthText: '登陆名不能超过30个字'
			        },{
						xtype: 'numberfield',
						name: 'sortNum',
						fieldLabel: '排序号',
						value: 1
					}
			    ],
			    firstFocus: function(){
			    	this.findField('departId').focus(false, 200);
			    },
			});
			return formPanel;
		},
		createButtons: function(){
			var buttons = [];
			if (!this.modifiedId){
				buttons.push({text: '保存并新增', width:90, handler: function(){this.save(true);}, iconCls: 'icon-save', scope: this});
			}
			buttons.push({text: '保存', width:60, handler: function(){this.save(false);}, iconCls: 'icon-save', scope: this});
			buttons.push({text: '关闭', width:60, handler: this.close, iconCls: 'icon-close', scope: this});
			return buttons;
		}
	});
	return UserWindow;
});